package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyActivity extends AppCompatActivity {

    private EditText userName = null;
    private EditText userPassword = null;
    private String nameTxt, pwdTxt;

    private RadioGroup gender = null;
    private String genderTxt;

    private CheckBox recreation, work, rest;
    private String recreationTxt, workTxt, restTxt;
    private String hobbyTxt;
    private Button btnRegister = null;
    private Button btnBack = null;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        userName = findViewById(R.id.userName);
        userPassword = findViewById(R.id.userPassword);

        gender = findViewById(R.id.gender);
        gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
//            radioGroup接收的是radioGroup对象本身, i 接收的是用户选择的RadioButton的id值
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i == R.id.male){
                    genderTxt = "男";
                }else if(i == R.id.female){
                    genderTxt = "女";
                }
            }
        });

        recreation = findViewById(R.id.recreation);
        work = findViewById(R.id.work);
        rest = findViewById(R.id.rest);

//        用一个监听器监听三个CheckBox
        CompoundButton.OnCheckedChangeListener ocl = new CompoundButton.OnCheckedChangeListener() {
            @Override
//            compoundButton传递CheckBox对象, b接收的是该对象是否被选中,true为选中
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (compoundButton.getId() == R.id.recreation){
                    if (b){
                        recreationTxt = "娱乐";
                    }else{
                        recreationTxt = null;
                    }
                }
                if (compoundButton.getId() == R.id.work){
                    if (b){
                        workTxt = "工作";
                    }else{
                        workTxt = null;
                    }
                }
                if (compoundButton.getId() == R.id.rest){
                    if (b){
                        restTxt = "休息";
                    }else{
                        restTxt = null;
                    }
                }

                hobbyTxt = "";
                if (recreationTxt != null){
                    hobbyTxt += recreationTxt;
                    hobbyTxt += " | ";
                }
                if (workTxt != null){
                    hobbyTxt += workTxt;
                    hobbyTxt += " | ";
                }
                if (restTxt != null){
                    hobbyTxt += restTxt;
                    hobbyTxt += " | ";
                }
//                去除后两个字符串
                if (hobbyTxt != ""){
                    hobbyTxt = hobbyTxt.substring(0, hobbyTxt.length()-2);
                }
            }
        };

        recreation.setOnCheckedChangeListener(ocl);
        work.setOnCheckedChangeListener(ocl);
        rest.setOnCheckedChangeListener(ocl);

        btnRegister = findViewById(R.id.btnRegister);
        btnBack = findViewById(R.id.btnBack);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                btnRegister.setText("已注册");
                nameTxt = userName.getText().toString();
                pwdTxt = userPassword.getText().toString();

                Toast.makeText(MyActivity.this, "用户名: " + nameTxt + " 密码: " + pwdTxt
                        + "\n性别: " + genderTxt + " 爱好: " + hobbyTxt, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MyActivity.this, ListViewActivity.class);
                startActivity(intent);
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}